package controller.api;

import java.util.Map;

import org.apache.commons.collections4.map.HashedMap;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Clear;
import com.jfinal.kit.HashKit;
import com.jfinal.kit.HttpKit;
import com.jfinal.kit.JsonKit;

import model.Wxuser;
import service.StationService;

@Clear
public class ApiTestController extends BaseApiController{

	/**
	 * SEVISER( 1 , "运维公司" ),
	STAFF( 2 , "运维人员" ),
	COMPANY( 3 , "企业" ),
	MANGER( 4 , "监管" );
	 */
	public void getTestUserInfo() {
//		renderJson("data" , Wxuser.dao.findById(1L)); //运维公司
		renderJson("data" , Wxuser.dao.findById(2L)); //运维人员
//		renderJson("data" , Wxuser.dao.findById(3L)); //企业
//		renderJson("data" , Wxuser.dao.findById(4L)); //监管
	}
	
	public void runTask() {
		new StationService().changeStatusTask();
		renderOk();
	}
	
	
	public void getNotify() {
		String res = getRawData();
		JSONObject jsonData = JSONObject.parseObject(res) ;
		String sign = jsonData.getString("sign") ;
		JSONObject order = jsonData.getJSONObject("order") ;
		
		System.out.println(order);
		System.out.println(sign);
		
		renderText("SUCCESS");
	}
	
	public void applyRefund() {
		Map<String, String> order = new HashedMap<String, String>();
		order.put("id", "603eee8c04fa2d0001f7d310");
		order.put("refundFee", "0.01");
		order.put("refundDesc", "退款原因是……");
		
		String signStr = "id=603eee8c04fa2d0001f7d310&refundDesc=退款原因是……&refundFee=0.01" ;
		String appSecret = "7dcd5fa3fa5fc2f4ed62491071ae4244" ;
		String sign = HashKit.md5(signStr + appSecret).toUpperCase() ;
		
		JSONObject data = new JSONObject();
		data.put("sign", sign);
		data.put("order", order);
		
		Map<String, String> headers = new HashedMap<String, String>();
		headers.put("Content-Type", "application/json");
		
		String url = "https://28c821d7-4c94-4918-8b2c-3b16eac0aab5.bspapp.com/http/curl/pay/applyRefund" ;
		String res = HttpKit.post(url, JsonKit.toJson(data) , headers);
		
		renderJson( JSONObject.parse(res) );
	}
	
}
